package bank.server;

import java.io.IOException;
import java.net.ServerSocket;

import bank.local.LocalBank;

public class BankServerSocket {
	// max Thread count
	public static int THREAD_POOL_SIZE = 10;
	// bank used by server
	private LocalBank bank;

	/**
	 * Main method of the bank server.
	 * 
	 * @param args Command line arguments. It's possible to define the port.
	 *            Else port 1234 will be used as default.
	 */
	public static void main(String[] args) {
		if (args.length == 1) {
			int port = Integer.parseInt(args[0]);
			if (port > 65535 || port <= 0) {
				port = 1234;
			}

			new BankServerSocket(port);
		}
	}

	/**
	 * Constructor.
	 * 
	 * @param port The port the server should listen on. Default port is 1234.
	 */
	public BankServerSocket(int port) {
		bank = new LocalBank();

		try {
			ServerSocket socket = new ServerSocket(port);
			socket.setSoTimeout(60000);

			for (int i = 0; i < THREAD_POOL_SIZE; i++) {
				Thread t = new Thread(new BankHandler(bank, socket));
				t.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
